package com.tuanzi.loan.business.service.family.impl;

import com.tuanzi.loan.business.entity.family.IcpWebSensitiveKeyword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tuanzi.loan.core.aspect.Debug;


import com.tuanzi.loan.business.repository.family.IcpWebSensitiveKeywordJpaRepository;

import com.tuanzi.loan.business.service.family.IcpWebSensitiveKeywordService;
import com.tuanzi.loan.vo.request.family.IcpWebSensitiveKeywordSearchRequest;
import com.tuanzi.loan.vo.response.family.IcpWebSensitiveKeywordSearchResponse;
import com.tuanzi.loan.business.repository.family.IcpWebSensitiveKeywordRepository;
import com.tuanzi.loan.core.vo.PageSearcher;
import org.springframework.data.domain.Page;
import java.util.Objects;

/**
 * ICP备案网站敏感字管理 ServiceImplement
 * @author JinRuiyang 2017/12/13 18:08:47
 */
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class IcpWebSensitiveKeywordServiceImpl implements IcpWebSensitiveKeywordService {

    @Autowired
    private IcpWebSensitiveKeywordRepository icpWebSensitiveKeywordRepository;

    @Autowired
    private IcpWebSensitiveKeywordJpaRepository icpWebSensitiveKeywordJpaRepository;


    /**
     * 更新敏感词的发布状态
     *
     * @param id     敏感词id
     * @param status 发布状态
     */
    @Override
    public void updatePublishStatus(Long id, Integer status) {
        if (Objects.isNull(id) || Objects.isNull(status))
            throw new IllegalArgumentException("参数 id 或 status 不能为空");
        IcpWebSensitiveKeyword sensitiveKeyword = icpWebSensitiveKeywordJpaRepository.findOne(id);
        if (Objects.isNull(sensitiveKeyword) || sensitiveKeyword.getIsEnable() == Disabled)
            throw new IllegalStateException("找不到该对象或其不可编辑");
        sensitiveKeyword.setStatus(status);
        icpWebSensitiveKeywordJpaRepository.save(sensitiveKeyword);
    }

    /**
     * 添加敏感词
     *
     * @param keyword 敏感词
     */
    @Override
    public void insert(String keyword) {
        if (Objects.isNull(keyword))
            throw new IllegalArgumentException("参数 keyword 不能为空");
        IcpWebSensitiveKeyword sensitiveKeyword = new IcpWebSensitiveKeyword();
        sensitiveKeyword.setKeyword(keyword);
        sensitiveKeyword.setStatus(UnPublish);
        sensitiveKeyword.setIsEnable(Enabled);
        icpWebSensitiveKeywordJpaRepository.save(sensitiveKeyword);
    }

    /**
     * 删除敏感词
     *
     * @param id 敏感词id
     */
    @Override
    public void delete(Long id) {
        if (Objects.isNull(id))
            throw new IllegalArgumentException("参数 id 不能为空");
        IcpWebSensitiveKeyword sensitiveKeyword = icpWebSensitiveKeywordJpaRepository.findOne(id);
        if (Objects.isNull(sensitiveKeyword) || sensitiveKeyword.getIsEnable() == Disabled)
            throw new IllegalStateException("找不到该对象或其不可删除");
        sensitiveKeyword.setIsEnable(Disabled);
        icpWebSensitiveKeywordJpaRepository.save(sensitiveKeyword);
    }

    /**
     * IcpWebSensitiveKeyword查询
     * @param pageSearcher 请求信息
     * @return 响应列表
     */
    public Page<IcpWebSensitiveKeywordSearchResponse> list(PageSearcher<IcpWebSensitiveKeywordSearchRequest> pageSearcher) {
        if (Objects.isNull(pageSearcher))
            throw new NullPointerException("pageSearcher is null");
        if (Objects.isNull(pageSearcher.getSearchCondition()) || Objects.isNull(pageSearcher.getPageable()))
            throw new NullPointerException("IcpWebSensitiveKeywordSearchRequest or pageable is null");
        return icpWebSensitiveKeywordRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }
}